package demo3;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {


    //求每一个数的平方和
    public int bitSum(int n) {
        int sum = 0;
        while (n > 0) {
            int t = n % 10;
            sum += t * t;
            n /= 10;
        }
        return sum;
    }

    public boolean isHappy(int n) {
        int slow = n, fast = bitSum(n);
        while (slow != fast) {
            slow = bitSum(slow);
            fast = bitSum(bitSum(fast));
        }
        return slow == 1;
    }


    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> ret = new ArrayList<>();
        char[] ss = s.toCharArray();
        char[] pp = p.toCharArray();
        int[] hash1 = new int[26];
        for (char c : pp) {
            hash1[c - 'a']++;  //统计p字符串中字符出现的次数
        }
        int[] hash2 = new int[26];
        int m = pp.length;
        for (int left = 0, right = 0; right < ss.length; right++) {
            char in = ss[right];
            hash2[in - 'a']++;   //入窗口
            if (right - left + 1 > m) {  //判断
                char out = ss[left];
                hash2[out - 'a']--;   //出窗口
                left++;
            }
            if (Arrays.equals(hash2, hash1)) {
                ret.add(left);       //更新结果
            }

        }
        return ret;
    }
    public static void main(String[] args) {

    }
}
